//
//  SFTabbar.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/5/24.
//

import UIKit

enum TabbarSelectState {
    case home
    case find
    case message
    case mine
}

class SFTabbar: UITabBar {

    var lastBtn: SFButton?
    var tabbarItems: [SFTabbarItem] = [SFTabbarItem]()
    var tabbarClickClosure: ((_ state: TabbarSelectState) -> Bool)?
    let states = [TabbarSelectState.home,TabbarSelectState.find,TabbarSelectState.message,TabbarSelectState.mine]
    var currentIndex: Int = 0
    
    override init(frame: CGRect){
        super.init(frame: frame)
        backgroundColor = UIColor.white
        setupTabbar()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupTabbar() {
        let btnW = Adapter.Screen.width / 4
        let btnH = 49
        
        for index in 0...states.count - 1 {
            let itemState = states[index]
            let tabbarItem = tabbarItemConfig(state: itemState)
            let itemBtn = createItemButton(item: tabbarItem)
            itemBtn?.frame = CGRect.init(x: Int(btnW) * index, y: 0, width: Int(btnW), height: btnH)
            itemBtn?.addTarget(target: self, action: #selector(tabbarItemBtnAction(item:)))
            itemBtn?.isSelect = index == 0
            itemBtn?.tag = 10000 + index
            addSubview(itemBtn ?? UIButton())
            tabbarItems.append(tabbarItem)
            if index == 0 { lastBtn = itemBtn }
        }
    }
    
    func tabbarItemConfig(state: TabbarSelectState) -> SFTabbarItem {
        var item: SFTabbarItem?
        
        switch state {
        case .home:
            item = SFTabbarItem.tabbarItem(title: "首页",
                                           normalImg: "icon_tabbar_home_normal",
                                           selectImg: "icon_tabbar_home_select",
                                           animateImg: "tabbar_homepage")
        case .find:
            item = SFTabbarItem.tabbarItem(title: "发现",
                                           normalImg: "icon_tabbar_find_normal",
                                           selectImg: "icon_tabbar_find_select",
                                           animateImg: "tabbar_discover")
        case .message:
            item = SFTabbarItem.tabbarItem(title: "消息",
                                           normalImg: "icon_tabbar_message_normal",
                                           selectImg: "icon_tabbar_message_select",
                                           animateImg: "tabbar_message")
        case .mine:
            item = SFTabbarItem.tabbarItem(title: "我的",
                                           normalImg: "icon_tabbar_mine_normal",
                                           selectImg: "icon_tabbar_mine_select",
                                           animateImg: "tabbar_mine")
        }
        return item!
    }

    func createItemButton(item: SFTabbarItem?) -> SFButton? {
        guard let m = item else { return nil }
        
        let btn = SFButton()
        btn.setTitle(m.title, .normal)
        btn.setTitleColor(kColor_ff3c39, .selected)
        btn.setTitleColor(kBlackColor_70, .normal)
        btn.setImage(UIImage(named: m.normalImg ?? ""), for: .normal)
        btn.setImage(UIImage(named: m.selectImg ?? ""), for: .selected)
        btn.buttonType = .txtBottom
        btn.titleLabel.font = UIFont.systemFont(ofSize: 10, weight: .regular)
        return btn
    }
    
    @objc func tabbarItemBtnAction(item: SFButton) {
        if item == lastBtn {return}
        let canClick: Bool = tabbarClickClosure!(states[item.tag - 10000])
        if canClick {
            
            lastBtn?.isSelect = false
            item.isSelect = true
            lastBtn = item
            
            currentIndex = item.tag - 10000
            // animate
            animateTabbarButton(item: item)
        }else {
            lastBtn?.isSelect = true
        }
    }
    
    func animateTabbarButton(item: SFButton) {
        let tabbarItem = tabbarItems[item.tag - 10000]
        let _ = SFAnimatedFrameImageView.animatedAt(inView: item.imageView,
                                            frame: item.imageView.frame,
                                            frameCount: 15,
                                            imageName:tabbarItem.animateImg,
                                            hideOrigin: true,
                                            finish: nil)
        
    }
    
    override func layoutSubviews() {
        superview?.layoutSubviews()
        
        for view in subviews {
            if NSStringFromClass(type(of: self)) == "UITabBarButton" {
                view.alpha = 0
            }
        }
    }
}

struct SFTabbarItem {
    var title: String?
    var normalImg: String?
    var selectImg: String?
    var animateImg: String?
    var selected: Bool?
    
    static func tabbarItem(title: String?,
                          normalImg: String?,
                          selectImg: String?,
                          animateImg: String?,
                          selected: Bool = false) -> SFTabbarItem {
        
        var item = SFTabbarItem()
        item.title = title ?? ""
        item.normalImg = normalImg ?? ""
        item.selectImg = selectImg ?? ""
        item.animateImg = animateImg ?? ""
        item.selected = selected
        return item
    }
}
